
package com.yimixian.app.rest.api;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.ImageInfo;
import com.yimixian.app.model.KV;
import com.yimixian.app.model.SearchKeyResult;
import com.yimixian.app.model.SessionDataBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * 活动专场数据
 */
public class SessionDataAPI extends YmxBaseAPI<SessionDataBean> {

    public static final String SESSIONDATAURL = "/v5/special_list";// 获取专场列表
    public String storeId;
    public String specialId;

    public SessionDataAPI(String storeId, String specialId, Callback<Response> callback) {
        super(callback);
        this.storeId = storeId;
        this.specialId = specialId;
    }

    public void getSessionData() {
        String token = DataManager.getInstance().get(CacheKeys.TOKEN);
        if (StringUtils.isEmpty(token)) token = "";
        if (!StringUtils.isEmpty(specialId)){
            mYmxRestService.getSessionData(token, storeId, specialId, mCallback);
        }else{
            mYmxRestService.getSessionData(token, storeId, mCallback);
        }
    }

    @Override
    public String getRelativeURL() {
        return SESSIONDATAURL;
    }

    @Override
    public SessionDataBean parseJson(JSONObject jsonObject) {
        SessionDataBean mSessionDataBean = null;
        if (jsonObject != null) {
            mSessionDataBean = new SessionDataBean();
            mSessionDataBean.cannotDeliverText = jsonObject.optString("cannot_deliver_text");
            if (!StringUtils.isEmpty(mSessionDataBean.cannotDeliverText)) return mSessionDataBean;
            mSessionDataBean.specialId = jsonObject.optString("special_id");
            mSessionDataBean.bannersImageRatio = jsonObject.optDouble("banners_image_ratio");

            JSONArray arraySpecials = jsonObject.optJSONArray("specials");
            if (arraySpecials != null) {

                List<KV> specials = new ArrayList<>();
                mSessionDataBean.specials = specials;
                for (int i = 0; i < arraySpecials.length(); i++) {
                    JSONObject object = arraySpecials.optJSONObject(i);
                    KV kv = new KV();
                    kv.keyId = object.optString("id");
                    kv.value = object.optString("name");
                    specials.add(kv);
                }
            }

            JSONArray arrayBanners = jsonObject.optJSONArray("banners");
            if (arrayBanners != null) {
                List<ImageInfo> imageInfos = new ArrayList<>();
                mSessionDataBean.banners = imageInfos;
                for (int i = 0; i < arrayBanners.length(); i++) {
                    JSONObject object = arrayBanners.optJSONObject(i);
                    ImageInfo imageInfo = new ImageInfo();
                    imageInfo.image = object.optString("image");
                    imageInfo.url = object.optString("url");
                    imageInfos.add(imageInfo);
                }
            }

            JSONArray arrayGoods = jsonObject.optJSONArray("goods");
            if (arrayGoods != null) {
                ArrayList<GoodsItem> goodsItems = new ArrayList<>();
                mSessionDataBean.goods = goodsItems;
                for (int i = 0; i < arrayGoods.length(); i++) {
                    JSONObject object = arrayGoods.optJSONObject(i);
                    GoodsItem goodsItem = new GoodsItem();
                    goodsItem.id = object.optInt("id");
                    goodsItem.name = object.optString("name");

                    if (!StringUtils.isEmpty(object.optString("img_ratio"))){
                        goodsItem.imgRatio = (float) object.optDouble("img_ratio");
                    }

                    goodsItem.img = object.optString("img");
                    goodsItem.promotionImg = object
                            .optString("promotion_img");
                    goodsItem.descUrl = object.optString("desc_url");
                    goodsItem.unitDesc = object.optString("unit_desc");
                    goodsItem.saleStatus = object.optString("sale_status");
                    goodsItem.unitPrice = object.optString("unit_price");
                    goodsItem.stockCount = object.optString("stock_count");
                    goodsItem.start_time = object.optLong("start_time");
                    goodsItem.countZeroReason = object.optString("count_zero_reason");
                    goodsItem.unitOldPrice = object
                            .optString("unit_old_price");
                    goodsItem.shouldBuyCount = object
                            .optInt("should_buy_count");
                    goodsItem.isBuyingGroup = object
                            .optInt("is_buying_group");
                    goodsItem.buyingGroupCount = object
                            .optInt("buying_group_count");
                    goodsItem.buyingGroupPrice = object
                            .optInt("buying_group_price");
                    goodsItems.add(goodsItem);
                }
            }
        }
        return mSessionDataBean;
    }
}
